# -*- coding: utf-8 -*-
import urllib, urllib2
import sys
import json
import sys;

reload(sys);
sys.setdefaultencoding("utf8")


def result(imageurl):
    loginUrl = 'https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=false&returnFaceLandmarks=false&returnFaceAttributes=age,gender,smile,glasses'
    key = '***********************'
    headers = {
        'Content-Type': 'application/json',
        'Host': 'api.projectoxford.ai',
        'Ocp-Apim-Subscription-Key': '***********************'#修改为自己的KEy
    }
    postdata = {
        'url': imageurl
    }
    req = urllib2.Request(url=loginUrl, data=json.dumps(postdata))
    req.add_header('Content-Type', 'application/json')
    req.add_header('Host', 'api.projectoxford.ai')
    req.add_header('Ocp-Apim-Subscription-Key', '212296bf1d224a7e89d8e38f5d49cb42')
    result = urllib2.urlopen(req).read()
    hjson = json.loads(result)
    smile = hjson[0]['faceAttributes']['smile']
    age = hjson[0]['faceAttributes']['age']
    glasses = hjson[0]['faceAttributes']['glasses']
    gender = hjson[0]['faceAttributes']['gender']
    smile = hjson[0]['faceAttributes']['smile']
    print hjson
    smiledes = ''
    if 0 <= smile < 0.30:
        smiledes = u'表情比较严肃的'
    if 0.30 <= smile < 0.60:
        smiledes = u'微笑的'
    if 0.60 <= smile < 0.80:
        smiledes = u'面露笑容的'
    if 0.80 <= smile < 0.95:
        smiledes = u'面露猥琐笑容的'
    if 0.95 <= smile <= 1.00:
        smiledes = u'笑成傻逼的'
    if glasses.lower() == u'noglasses':
        glasses = u'不戴眼镜的'
    if glasses.lower() == u'readingglasses':
        glasses = u'戴着眼镜的'
    if glasses.lower() == u'sunglasses':
        glasses = u'戴着墨镜的'
    if glasses.lower() == u'swimminggoggles':
        glasses = u'戴着泳镜的'
    if gender == 'male':
        gender = '帅哥'
    else:
        gender = '美女'
    return '经鉴定:' + '这是一位' + glasses + smiledes + gender + ',年龄大约为:' + str(age) + '岁.微笑指数:' + str(
        smile) + '\n\n本图片由微软&汪欣提供识别服务'


if __name__ == '__main__':
    imageurl = 'https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D360/sign=5ec76a78d8c451dae9f60aed86fd52a5/dbb44aed2e738bd4f60e4017a48b87d6277ff9ed.jpg'
    print result(imageurl)
